package cz.najman.emdroid.helper;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import cz.najman.emdroid.connection.ConnectionMethod;
import cz.najman.emdroid.mappings.User;
import cz.najman.emdroid.service.ForwardingResultReceiver;
import cz.najman.emdroid.service.UpdaterService;

/**
 * @author Lukáš Najman
 */
public class UserHelper extends AbstractHelper {

	private static final String TAG = "UserHelper";	
	/**
	 */
	User mUser = new User();

	public UserHelper(Context context, ForwardingResultReceiver receiver) {
		super(context, receiver);
	}

	public void retrieveUserByEmail(String email) {
		Log.d(TAG, "> retrieveUsersByEmail");
		Intent intent = getIntentWithExtra(
				Uri.withAppendedPath(User.CONTENT_URI_USERS_BY_EMAIL, email),
				ConnectionMethod.METHOD_GET,
				new String[] {User.URI_TEMPLATE_STRING_EMAIL},
				new String[] {email},
				null
		);
		mContext.startService(intent);
	}

	public void retrieveUserById(Integer userId) {
		Log.d(TAG, "> retrieveUsersById");
		Intent intent = getIntentWithExtra(
				Uri.withAppendedPath(User.CONTENT_URI_USERS, userId.toString()),
				ConnectionMethod.METHOD_GET,
				new String[] {User.URI_TEMPLATE_STRING},
				new String[] {userId.toString()},
				null
		);
		mContext.startService(intent);
	}
	
	@Override
	protected Intent getIntentWithExtra(Uri uri, int method, String[] parameterTypes, String[] parameterValues, ContentValues values) {
		Intent intent = new Intent(mContext, UpdaterService.class);
		intent.putExtra(UpdaterService.RESULT_RECEIVER,	mResultReceiver);
		intent.putExtra(UpdaterService.OBJECT, User.class);
		intent.putExtra(UpdaterService.URI, uri);
		intent.putExtra(UpdaterService.METHOD, method);
		if (parameterTypes != null) {
			intent.putExtra(UpdaterService.PARAMETER_TYPES, parameterTypes);
			intent.putExtra(UpdaterService.PARAMETER_VALUES, parameterValues);
		}
		if (values != null) {
			intent.putExtra(UpdaterService.CONTENT_VALUES, values);
		}

		return intent;
	}
	
	
	public Cursor getUserById(int userId, String[] projection){
		return mContentResolver.query(Uri.withAppendedPath(User.CONTENT_URI_USERS, "" + userId), projection, null, null, null);
	}
}
